<template>
  <div class="bg-ff p-20">
    <!-- 查找表单 -->
    <el-form :inline="true" :model="queryParams" ref="queryForm">
      <el-form-item label="角色名称" prop="name">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="角色名称" v-model="queryParams.name" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" plain type="primary">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-20">
      <el-button @click="handleAdd" icon="el-icon-plus" plain type="primary">新增</el-button>
      <el-button :disabled="multiple" @click="handleDelete" icon="el-icon-delete" plain type="danger">删除</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :data="pageList"
      @row-click="handleRowClick"
      @selection-change="handleSelectionChange"
      border
      highlight-current-row

      v-loading="loading"
    >
      <el-table-column align="center" type="selection" />
      <el-table-column :width="60" align="center" label="序号" type="index" />
      <el-table-column label="角色名称" prop="name" />
      <el-table-column label="排序" prop="sort" />
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <el-switch :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row)" v-model="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button @click.stop="handleUpdate(scope.row)" circle icon="el-icon-edit" plain type="primary" />
          <el-button @click.stop="handleDelete(scope.row)" circle icon="el-icon-delete" plain type="danger" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :size.sync="pagination.size"
      :page.sync="pagination.current"
      :total="pagination.total"
      @pagination="handleQuery"
      v-show="pagination.total>0"
    />

    <!-- 角色弹窗 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="30%">
      <el-form :model="form" :rules="rules" @close="closeDialog" label-width="25%" ref="form">
        <el-form-item label="角色名称" prop="name">
          <el-input placeholder="请输入角色名称" v-model="form.name" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number :min="0" controls-position="right" style="width: 100px" v-model="form.sort" />
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="handleSubmit" type="primary">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Api from '../api'
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      queryParams: {
        queryMode: '0',
        name: undefined
      },
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      // 菜单列表
      menuOptions: [],
      // 表单参数
      form: {
        sort: 1,
        status: 1
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      permissionDialog: {
        visible: false
      },
      permissionForm: {
        id: undefined,
        name: undefined,
        permissionIds: []
      },
      permissionRule: {},
      permissionOptions: []
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.current = this.pagination.current
      this.queryParams.size = this.pagination.size
      Api.getRoleList(this.queryParams).then(data => {
        this.pageList = data.records
        this.pagination.total = data.total
      }).finally(() => { this.loading = false })
    },
    handleReset() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0
      }
      this.queryParams.name = undefined
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleStatusChange(row) {
      const text = row.status === 1 ? '启用' : '禁用'
      this.$confirm('确认要' + text + '"' + row.name + '"吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return Api.addOrUpdateRole({ ...row, status: row.status })
      }).then(() => {
        this.$message.success(text + '成功')
      }).catch(function () {
        row.status = row.status === 1 ? 0 : 1
      })
    },
    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增角色',
        visible: true
      }
    },
    handleUpdate(row) {
      this.resetForm()
      this.dialog = {
        title: '修改角色',
        visible: true
      }
      this.form = JSON.parse(JSON.stringify(row))
    },
    handleDelete(row) {
      const ids = row && row.id ? row.id : [this.ids]
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Api.removeRoles(ids).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      }).catch(() =>
        this.$message.info('已取消删除')
      )
    },
    handleSubmit: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          Api.addOrUpdateRole(this.form).then(() => {
            this.$message.success(this.form.id ? '修改成功' : '新增成功')
            this.dialog.visible = false
            this.handleQuery()
          })
        }
      })
    },
    resetForm() {
      this.form = {
        sort: 1,
        status: 1
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    },
    handleRowClick(row) {
      this.$emit('roleClick', row)
    }
  }
}
</script>
